#来源：http://www.myexception.cn/network/1990714.html
import matplotlib.pyplot as plt
import networkx as nx

G = nx.Graph()
#为图添加标签。
G.add_edge('a', 'b', weight=0.6)
G.add_edge('a', 'c', weight=0.2)
G.add_edge('c', 'd', weight=0.1)
G.add_edge('c', 'e', weight=0.7)
G.add_edge('c', 'f', weight=0.9)
G.add_edge('a', 'd', weight=0.3)


def 获取边的权重():
    edgeLabels = {}
    for u, v, weightDict in G.edges(data=True):
        edgeLabels[(u, v)] = weightDict['weight']
    return edgeLabels


def 根据权重大小将边分类(threshold: int) -> list:
    elarge = []
    esmall = []
    for (u, v, d) in G.edges(data=True):
        if (d['weight'] > threshold):
            elarge.append((u, v))
        else:
            esmall.append((u, v))
    return esmall, elarge


esmall, elarge = 根据权重大小将边分类(threshold=0.2)
pos = nx.spring_layout(G)  # positions for all nodes

# 绘制节点
nx.draw_networkx_nodes(G, pos, node_size=700)

# 绘制连线。
nx.draw_networkx_edges(G, pos, edgelist=elarge,
                       width=6)
nx.draw_networkx_edges(G, pos, edgelist=esmall,
                       width=6, alpha=0.5, edge_color='b', style='dashed')
# 绘制边的标签
nx.draw_networkx_edge_labels(G, pos, edge_labels=获取边的权重())

# 绘制节点的标签
nx.draw_networkx_labels(G, pos, font_size=20, font_family='sans-serif')

plt.axis('off')
plt.show()
